package cn.edu.njuit.webserver.securityserver.apis;

import cn.edu.njuit.webserver.securityserver.entity.UserAccount;
import cn.edu.njuit.webserver.securityserver.service.AccountService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class AccountController {
    private final AccountService accountService;

    public AccountController(AccountService accountService) {
        this.accountService = accountService;
    }

    @ApiOperation("注册新账户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "login", value = "登录名", required = true),
            @ApiImplicitParam(name = "password", value = "登录密码", required = true),
            @ApiImplicitParam(name = "defaultRole", value = "默认角色名", required = false)
    })
    @PostMapping("/regist")
    public ResponseEntity registerAccount(@RequestParam(required = true)String login,
                                          @RequestParam(required = true)String password,
                                          @RequestParam(required = false,defaultValue = "role_user")String defaultRole) {
        try {
            UserAccount account=this.accountService.regAccount(login,password,defaultRole);
            return ResponseEntity.ok(account);
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject error=new JSONObject();
            error.put("errorInfo",e.getMessage());
            return ResponseEntity.status(400).body(error);
        }
    }

    @ApiOperation("账户登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "login", value = "登录名", required = true),
            @ApiImplicitParam(name = "password", value = "登录密码", required = true)
    })
    @PostMapping("/login")
    public ResponseEntity doLogin(@RequestParam(required = true)String login,
                                  @RequestParam(required = true)String password){

        try{
            String token=this.accountService.doLogin(login,password);
            JSONObject result=new JSONObject();
            result.put("id_token",token);
            return ResponseEntity.ok(result);
        }catch (Exception e){
            e.printStackTrace();
            JSONObject error=new JSONObject();
            error.put("errorInfo",e.getMessage());
            return ResponseEntity.status(400).body(error);
        }
    }

    @ApiOperation("分页获取全部账户")
    @GetMapping("/accounts")
    public ResponseEntity getAccounts(@RequestParam(required = false,defaultValue = "0")Integer page,
                                      @RequestParam(required = false,defaultValue = "10")Integer size){
        Page<UserAccount> result=this.accountService.getAllAccounts(page,size);
        return ResponseEntity.ok(result);
    }

    @DeleteMapping("/accounts")
    public ResponseEntity deleteAccount(@RequestParam(required = true)String login){
        try {
            boolean result=this.accountService.deleteAccount(login);
            return ResponseEntity.ok("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject error=new JSONObject();
            error.put("errorInfo",e.getMessage());
            return ResponseEntity.status(400).body(error);
        }

    }
}
